package common;

import java.util.List;

import dict.TransactionDict;
import net.sf.json.JSONObject;
import utils.RegularUtil;
import utils.StringUtil;

public class Validate {

	// 不能为空
	public static int NOTEMPTY = 0x00000001;
	// 不能为空
	public static int CHARNUMBER = 0x00000010;
	// 长度是否在范围内
	public static int EXCEEDLENGTH = 0x00000100;
	// 是否数字
	public static int ISNUMBER = 0x00001000;
	// 是否在范围内
	public static int EXCEEDNUMBER = 0x00010000;
	// 是否http开头
	public static int HTTPHEADER = 0x00100000;
	// 是否不http开头
	public static int NOHTTPHEADER = 0x01000000;
	// 是否邮箱格式
	public static int EMAILFORMAT = 0x10000000;

	public static JSONObject validate(List<Object[]> parameterList) {
		for (Object[] args : parameterList) {
			String name = (String) args[0];
			String value = (String) args[1];
			int mode = Integer.valueOf(args[2].toString());
			int top = Integer.valueOf(args[3].toString());
			int end = Integer.valueOf(args[4].toString());
			if ((NOTEMPTY & mode) != 0) {
				if (StringUtil.isEmpty(value)) {
					return result(TransactionDict.ARGS_EMPTY, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_EMPTY));
				}
			}
			if ((CHARNUMBER & mode) != 0) {
				if (!StringUtil.isEmpty(value) && !RegularUtil.test(value, RegularUtil.CHARNUMBER)) {
					return result(TransactionDict.ARGS_FORMAT, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_FORMAT));
				}
			}
			if ((EXCEEDLENGTH & mode) != 0) {
				if (!StringUtil.isEmpty(value) && (value.length() <= top || value.length() > end)) {
					return result(TransactionDict.ARGS_EXCEED, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_EXCEED));
				}
			}
			if ((ISNUMBER & mode) != 0) {
				if (!StringUtil.isNumeric(value)) {
					return result(TransactionDict.ARGS_NUMBER, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_NUMBER));
				}
			}
			if ((EXCEEDNUMBER & mode) != 0) {
				if (StringUtil.isNumeric(value) && (Double.parseDouble(value) <= top || Double.parseDouble(value) > end)) {
					return result(TransactionDict.ARGS_EXCEED, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_EXCEED));
				}
			}
			if ((HTTPHEADER & mode) != 0) {
				if (!StringUtil.isEmpty(value) && !value.startsWith("http")) {
					return result(TransactionDict.ARGS_FORMAT, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_FORMAT));
				}
			}
			if ((NOHTTPHEADER & mode) != 0) {
				if (!StringUtil.isEmpty(value) && value.startsWith("http")) {
					return result(TransactionDict.ARGS_FORMAT, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_FORMAT));
				}
			}
			if ((EMAILFORMAT & mode) != 0) {
				if (!StringUtil.isEmpty(value) && !RegularUtil.test(value, RegularUtil.EMAIL)) {
					return result(TransactionDict.ARGS_FORMAT, "[" + name + "]" + TransactionDict.MESSAGES.get(TransactionDict.ARGS_FORMAT));
				}
			}
		}
		return null;
	}

	public static JSONObject result(String code, String message) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("message", message);
		return jsonObject;
	}
	
	public static String summary(List<Object[]> parameterList) {
		StringBuffer sb=new StringBuffer();
		for (Object[] args : parameterList) {
			String name = (String) args[0];
			String value = (String) args[1];
			sb.append(name+" "+value+"|");
			
		}
		return sb.toString();
	}

	public static void main(String[] args) {

	}
}
